<template>
	<view>
		<view>
			<view class="show-buka" v-if="join_time != ''">申请补卡日期：{{join_time}}</view>
			
			<view class="" v-for="item in list">
				<view class="" v-for="(item,index) in item.contentList">
					{{item.content}}
				</view>
				<preview-form ref="preview" :dataList="item.dataList" v-if="list.length > 0" :default_user_index="default_user_index"></preview-form>
			</view>
			<view class="button" @tap="submit" v-if="showSubmit == true">立即参加</view>
			<view class="zhanwei"></view>
		</view>
		
	</view>
</template>

<script>
	import previewForm from "@/components/diy-form/preview-form.vue";
	export default {
		components:{
			previewForm,
		},
		data() {
			return {
				showSubmit:false,
				s_id:'',
				join_time:'',
				list:[],
				detailInfo:{},
				default_user_index:-1,
			}
		},
		onLoad(params) {
			//params.s_id = 9;
			if(params.default_user_index)
			{
				this.default_user_index = params.default_user_index;
			}
			if(params.join_time)
			{
				this.join_time = params.join_time;
			}
			if(params.s_id)
			{
				this.s_id = params.s_id;
				this.getInfo();
			}
		},
		methods: {
			async submit(){
				var that = this;
				var checkLogin = that.$api.checkLogin(true);
				if(checkLogin == false)
				{
					that.$api.navigateTo('/pages/member/login');return ;
				}
				//this.$refs.preview.uploadFile(); //检测上传文件
				that.$api.showModal('确认参加','',async function(res){
					if(res == 'yes')
					{
						
						let resData = []
						
						// 使用 Promise.all 并行执行异步操作
						await Promise.all(that.list.map(async (val, index) => {
						  const obj = await that.$refs.preview[index].getFormData();
						  resData.push(obj);
						}));
						
						
						
		
						// if(that.detailInfo.join_datalist != '')
						// {
						// 	var resData = await that.$refs.preview.getFormData(); //获取最终数据方法
						// 	// console.log('join resData',resData);
						// 	if(!resData || resData.length < 1)
						// 	{
						// 		return false;
						// 	}
						// }else{
						// 	var resData = '';
						// }
						// 数组转换为字符串的一种方法是在其后连接一个空字符串
						// var need_check_text = resData+ "";
						// if(need_check_text)
						// {
						// 	var res = await that.$api.checkQiniuText(need_check_text);
						// 	console.log("校验结果"+JSON.stringify(res))
						// 	if(!res.status)
						// 	{
						// 		return ;
						// 	}
						// }
						that.$api.loadingMsg('正在提交数据');
						that.$http.post('statistics/joinStatistics',{
							resData,
							s_id:that.s_id,
							join_time:that.join_time
						}).then(res=>{
							that.$api.msg(res.msg)
							if(res.code == 1)
							{
								that.showSubmit = false;
								setTimeout(function(){
									that.$api.navbackTo();
								},2000)
							}
						})
					}
				})
				//var check = this.$refs.preview.checkFormData();  //检测填写数据是否完善
				//console.log(resData)
			},
			getInfo(){
				var that = this;
				if(that.s_id > 0) 
				{
					
					// that.$http.post('statistics/getStatisticsDetailInfo1',{
					// 	s_id:that.s_id,
					// 	join_time:that.join_time
					// }).then(res=>{
					// 	if(res.code == 1)
					// 	{
					// 		var detailInfo = res.data.detailInfo;
					// 		// that.detailInfo = detailInfo;
					// 		if(detailInfo.join_datalist)
					// 		{
					// 			that.list = JSON.parse(detailInfo.join_datalist)
					// 		}
						
					// 		that.showSubmit = true;
					// 	}else{
					// 		that.$api.msg(res.msg);
					// 	}
					// })
					
					that.$http.post('statistics/getStatisticsDetailInfo',{
						s_id:that.s_id,
						join_time:that.join_time
					}).then(res=>{
						if(res.code == 1)
						{
							var detailInfo = res.data.data;
							// that.detailInfo = detailInfo;
							// if(detailInfo.join_datalist)
							// {
							// 	that.list = JSON.parse(detailInfo.join_datalist)
							// }
							that.list = detailInfo
							console.log(detailInfo,'detailInfo')
							// if(detailInfo.last_content)
							// {
							// 	setTimeout(function(){
							// 		var contentArr = JSON.parse(detailInfo.last_content);
							// 		that.$refs.preview.saveInitResultData(contentArr)
							// 	},100)
							// }
							that.showSubmit = true;
						}else{
							that.$api.msg(res.msg);
						}
					})
				}
			},
			
		}
	}
</script>

<style lang="scss">
	.show-buka{
		padding: 20rpx 0 0 20rpx;
		color: #999999;
		font-size: 26rpx;
	}
	.show-name{
		background: #FFFFFF;
		    width: 94%;
		    margin: 20rpx 3%;
		    border-radius: 20rpx;
		    line-height: 40px;
		    height: 80rpx;
		    overflow: hidden;
			text-align: center;
			color: #999999;
	}
	.button{
		height: 80rpx;
		width: 90%;
		margin: 50rpx auto;
		background-color: $uni-btn-color;
		color: #ffffff;
		text-align: center;
		line-height: 80rpx;
		letter-spacing: 2rpx;
		border-radius: 30rpx;
	}
	.show-kl{
		.kouling{
			width: 100%;
			height: 100rpx;
			background: #ffffff;
			margin: 20rpx 0;
			.input{
				width: 100%;
				padding-left: 40rpx;
				height: 100rpx;
			}
		}
	}
	
</style>
